#Python 3.9

import time
import requests
import os
import time
import subprocess

ver = ['10001','0.98-Beta']
n1 = "https://gitee.com/summer-uefi/yuanshenqidong-command/raw/master/version"
n2 = "https://gitee.com/summer-uefi/yuanshenqidong-command/raw/master/version-info"

def checkupdate():
    v1 = requests.get(n1)
    #print(v1.text)
    v2 = requests.get(n2)
    #print(v2.text)
    if(ver[0] == v1.text or ver[1] == v2.text):
        return True
    else:
        return False
def update():
    ak = checkupdate()
    if(ak == True):
        print("[UPDATEC]当前版本无需更新！版本号"+ ver[0],'内部编号：'+ver[1])
    else:
        print('发现新版本，请复制链接到浏览器下载更新：https://ccjjyy.lanzoum.com/b0ehxk0kf \n密码：519c')
def verify():
    try:
        a = open('path.config','r',encoding='utf-8')
        aread = a.readline()
        if('Error' in aread):
            return False
        else:
            return True
    except:
        print("[LOG]请稍等，正在自动获取原神位置。")
        os.system("start GetLocate.exe")
        time.sleep(1.5)
def whereis():
    a = open('path.config', 'r',encoding='utf-8')
    aread = a.readline()
    print("[LOG]已获取Launcher路径："+aread)
    return aread
def readini(cfgpath):
    print('[LOG]Config文件：'+cfgpath)
    aq = open(cfgpath,'r',encoding='utf-8')
    asq = aq.readlines()
    asq = asq[1]
    return asq

def qidong(command):
    time.sleep(1)
    subprocess.Popen(command)
def zhongzhi(quitcommand):
    os.system(quitcommand)

def restart(quitcommand,command):
    os.system(quitcommand)
    time.sleep(1)
    subprocess.Popen(command)

def inputcomm(a1,a2,flag):
    if(flag):
        print('[CONSOLE]GLCCSL -> USRCSL')
    while True:
        flag = False
        in1 = input('[USR][INPUT]:')
        if(in1 == 'k' or in1 == 'K'):
            zhongzhi(a1)
            time.sleep(1)
            os.system('cls')
            inputcomm(a1,a2,flag)
        elif(in1 == 'q' or in1=='Q'):
            quit('User-Quit')
            break
        elif(in1 == 'c' or in1=='C'):
            try:
                os.remove('path.config')
                print('[LOG]Success!')
                time.sleep(1)
                os.system('cls')
            except:
                print("[SYS][ERR]没有指定文件")
                break
                return 1
        elif(in1 == 'r' or in1 == 'R'):
            print('[LOG]重启原神')
            restart(a1,a2)
            inputcomm(a1,a2,flag)
        else:
            inputcomm(a1,a2,flag)

if __name__ == "__main__":
    print('[OUTPUT]原神，启动！')
    print("[UPDATE]正在检测版本更新！");time.sleep(0.5);update()
    o = verify()
    if(o == False):
        print("[LOG][ERROR]无法读取目录。请联系开发者处理。")
    else:
        path = whereis()
    cfgpath = path + '\\Config.ini'
    aqa = readini(cfgpath)
    aqa = aqa.split('=')
    aqa = aqa[1]
    aqa = aqa.split('\n')
    aqa = aqa[0]
    aqa = aqa + '/YuanShen.exe'
    command = '\"' + aqa + '\"'
    print('[LOG]已获取原神路径' + command)
    qidong(command)
    quitcommand = "taskkill /f /im YuanShen.exe"
    print("[LOG]准备完成！原神将在数秒内启动。启动后输入k可结束原神进程，输入q退出启动器。输入r重启原神\n[TEST]输入C删除path文件")
    flag = True
    inputcomm(quitcommand,command,flag)